package MemberController;

import java.io.*;
import java.util.*;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class MemberDelAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
    public MemberDelAction() {
        super();
       
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("*****회원탈퇴 서블릿******");
	
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		
		RequestDispatcher view = null;
		PrintWriter out =response.getWriter();
		String action_code = request.getParameter("action_code");
		
		MembersBean mbean = new MembersBean();
		MembersDAO mdao = new MembersDAO();
		if(action_code.equals("Delete")){
			String numMode = request.getParameter("num");
			int start = numMode != null ? Integer.parseInt(numMode) : -1;
				
				switch(start){
				
				case 1:
					
					System.out.println("*****탈퇴페이지이동*****");
					request.setAttribute("upcheck","4");
					request.setAttribute("check","/Main/FirstMain/Fm_Delete.jsp");
					view = request.getRequestDispatcher("/Main/FirstMain/Fm_Update.jsp");
					view.forward(request, response);
					break;
				
				case 2:
					
					System.out.println("*****탈퇴성공*****");
					boolean check = false;
					
					mbean.setMem_Nick(request.getParameter("Mem_Nick"));
					mbean.setMem_Pass(request.getParameter("Mem_Pass"));
					mbean.setMem_Email(request.getParameter("Mem_Email"));
					
					System.out.println(request.getParameter("Mem_Nick"));
					System.out.println(request.getParameter("Mem_Pass"));
					System.out.println(request.getParameter("Mem_Email"));
					
					check = mdao.mdeleteC(mbean);
					
						if(check == true){
							mdao.mdelete(mbean);
							out.println( "<script>" );
							out.println( "alert(\"탈퇴가 완료되었습니다!감사합니다!\");" );
							out.println( "location.href=\"./index.jsp\";" );
							out.println( "</script>" );
							break;
							
						}else{
							out.println( "<script>" );
							out.println( "alert(\"일치하는 정보가 없습니다!\");" );
							out.println( "location.href=\"javascript:history.go(-1)\";" );
							out.println( "</script>" );
							break;
						}
					
				}
			}
		}
	}
